<template>
  <div class="app-body">
    <div class="common-level-rail"><el-button size="small" type="primary" icon="el-icon-plus" @click="addClick">添加商品</el-button></div>
    <div class="table-wrap">
      <el-table :data="tableData" style="width: 100%" v-loading="loading">
        <el-table-column prop="zone_product_id" label="ID" width="60"></el-table-column>
        <el-table-column prop="product_image" label="封面" width="50">
          <template slot-scope="scope">
            <img v-if="scope.row.product_image" :src="scope.row.product_image" width="30" height="30" />
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column prop="product_name" label="商品名称">
          <template slot-scope="scope">
            <div class="text-ellipsis" :title="scope.row.product_name">{{scope.row.product_name}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="shop_name" label="店铺" width="160"></el-table-column>
        <el-table-column prop="virtual_views" label="实际浏览" width="100"></el-table-column>
        <el-table-column prop="sort" label="排序" width="100"></el-table-column>
        <el-table-column prop="update_time" label="更新时间" width="140"></el-table-column>
        <el-table-column prop="name" label="操作" width="90">
          <template slot-scope="scope">
            <el-button @click="editProduct(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="deleteProduct(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          :current-page="curPage"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="totalDataNumber"
        ></el-pagination>
      </div>
    </div>
    <!--选择商品-->
    <Product :isproduct="isproduct" @closeDialog="closeProductDialogFunc($event)">产品列表</Product>
  </div>
</template>

<script>
import Product from '@/components/product/AssembleProduct';
import HomeproductApi from '@/api/homeproduct.js';
export default {
  components: {Product},
  data() {
    return {
      isproduct: false,
      product_arr: [],
      /*分类*/
      categoryData: [],
      /*表单数据*/
      tableData: [],
      /*是否打开添加弹窗*/
      open_add: false,
      /*是否打开编辑弹窗*/
      open_edit: false,
      /*当前编辑的对象*/
      userModel: {},
      commentData: [],
      loading: true,
      /*一页多少条*/
      pageSize: 20,
      /*一共多少条数据*/
      totalDataNumber: 0,
      /*当前是第几页*/
      curPage: 1
    };
  },
  created() {
    /*获取文章列表*/
    this.getTableList();
  },
  methods: {

    /*获取文章列表*/
    getTableList() {
      let self = this;
      let Params = {};
      Params.page = self.curPage;
      Params.list_rows = self.pageSize;
      HomeproductApi.productlist(Params, true)
        .then(data => {
          self.loading = false;
          self.tableData = data.data.list.data;
          self.totalDataNumber = data.data.list.total;
        })
        .catch(error => {
          self.loading = false;
        });
    },

    /*选择第几页*/
    handleCurrentChange(val) {
      let self = this;
      self.curPage = val;
      self.loading = true;
      self.getTableList();
    },

    /*每页多少条*/
    handleSizeChange(val) {
      this.curPage = 1;
      this.pageSize = val;
      this.getTableList();
    },

    /*删除专区商品*/
    deleteProduct(row) {
      let self = this;
      self
        .$confirm('确定执行删除操作?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        .then(() => {
          self.loading = true;
          HomeproductApi.deleteProduct(
            {
              productId: row.home_product_id
            },
            true
          )
            .then(data => {
              self.$message({
                message: data.msg,
                type: 'success'
              });
              self.loading = false;
              self.getTableList();
            })
            .catch(error => {});
        })
        .catch(() => {});
    },
    /*编辑文章*/
    editProduct(row) {
      let home_product_id = row.home_product_id;
      this.$router.push({
        path: '/market/home/edit',
        query: {
          productId: home_product_id
        }
      });
    },
    handleClick(tab, event) {},
    /*添加商品*/
    addClick() {
      this.isproduct = true;
    },
    /*关闭弹窗*/
    closeDialogFunc(e, f) {
      if (f == 'add') {
        this.open_add = e.openDialog;
        if (e.type == 'success') {
          this.getTableList();
        }
      }
      if (f == 'edit') {
        this.open_edit = e.openDialog;
        if (e.type == 'success') {
          this.getTableList();
        }
      }
    },
    /*关闭弹窗*/
    closeProductDialogFunc(e) {
      this.isproduct = e.openDialog;
      // console.log(e.params);
      if (e.type == 'success') {
        let param = {
          assemble_product_id:e.params.assemble_product_id,
          product_name:e.params.product_name,
          product_image:e.params.product_image,
          shop_name:'',
          sort : 1
        };
        sessionStorage.setItem('ASSEMBLE_PRODUCT', JSON.stringify(param) );
        this.$router.push({
            path: '/market/home/add'
        });
      }
    }
  }
};
</script>

<style scoped>
.app-body{
  padding-bottom: 100px;
}
</style>
